package com.starnet.yarnmonitor.yarn.controller;

import com.starnet.yarnmonitor.yarn.handler.ApiException;
import com.starnet.yarnmonitor.yarn.vo.result.Result;
import com.starnet.yarnmonitor.yarn.entity.AppTrackLog;
import com.starnet.yarnmonitor.yarn.service.SubmissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/submission")
public class SubmissionController {
    @Autowired
    SubmissionService submissionService;
    @ApiOperation(value = "提交jar包自动运行")
    @PostMapping
    public Result submitJar(@RequestPart MultipartFile multipartFile, @RequestParam(value = "arguments",required = false)String arguments) throws IOException {
        AppTrackLog appTrackLog = submissionService.submitJar(multipartFile, arguments);
        return Result.success(appTrackLog);
    }

    @ApiOperation(value = "查看jar包运行状态")
    @GetMapping("/{trackId}")
    public Result trackById(@PathVariable("trackId")Long trackId) throws IOException {
        String log = submissionService.trackById(trackId);
        if (log==null||log.isEmpty()){
            throw new ApiException("程序还在运行中,请稍后查看");
        }
        return Result.success(log);
    }
}
